<?php

namespace Admin\Controller;
use Think\Controller;

class ModuleController extends AdminBasicController{

	public $Module = '';
	public $Project = '';
	
	public function _initialize(){
		$this->checkLogin();
		$this->Module = D('Module');
		$this->Project = D('Project');
	}

	/**
	 * 模块列表
	 */
	public function moduleList(){
		$where = array();
		if(isset($_POST['mo_name'])){
			$where['mo_name'] = array('like','%'.$_POST['mo_name'].'%');
		}
		$module_list = $this->Module->selectModule($where,'ctime desc',10);
		//获取分页
		$page = $module_list['page'];
		//获取分页数据
		$module_list = $module_list['list'];
		foreach ($module_list as $k => $v) {
			$where['p_id'] = $v['p_id'];
			$module_list[$k]['p_name'] = $this->Project->findProject($where);
		}
		$this->assign('module_list',$module_list);
		$this->assign('page',$page);
		$this->display('Module/moduleList');
	}


	/**
	 * 增加模块
	 */
	public function addModule(){
		if(empty($_POST)){
			$project_list = $this->Project->selectProject('','ctime desc','');
			$this->assign('project_list',$project_list);
			$this->display('Module/addModule');
		}else{
			if($data=$this->Module->create()){
				$data['ctime'] = time();
				$back = $_POST['back'];
				if($this->Module->addModule($data)){
					$this->success('添加模块成功',$back);
				}else{
					$this->success('添加失败');
				}
			}else{
				$this->error($this->Module->getError());
			}
		}
	}

	/**
	 * 编辑模块
	 */
	public function editModule(){
		if(empty($_POST)){
			$where['mo_id'] = I('get.mo_id');
			$module_find = $this->Module->findModule($where);
			$project_list = $this->Project->selectProject('','ctime desc','');
			$this->assign('project_list',$project_list);
			$this->assign('module_find',$module_find);
			$this->display('Module/editModule');
		}else{
			if($data=$this->Module->create()){
				$back = $_POST['back'];
				if($this->Module->editModule(array('mo_id'=>$_POST['mo_id']),$data)){
					$this->success('编辑模块成功',$back);
				}else{
					$this->success('编辑失败');
				}
			}else{
				$this->error($this->Module->getError());
			}
		}
	}

}


?>